﻿using System;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using ComApp.Model;
using ComApp;
using Android.Util;
using Android.Graphics;
using System.Timers;

namespace AndroidApp
{
    public class GifView : ImageView
    {
        private bool isGifImage;
        private int image;
        private Movie movie;
        private long movieStart = 0;

        public GifView(Context context, IAttributeSet attrs)
            : base(context, attrs)
        {
            //获取自定义属性isgifimage
            var array = context.ObtainStyledAttributes(attrs, Resource.Styleable.GifView);
            isGifImage = array.GetBoolean(Resource.Styleable.GifView_isGifImage, true);
            array.Recycle();//获取自定义属性完毕后需要recycle，不然会对下次获取造成影响
            //获取ImageView的默认src属性
            image = attrs.GetAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0);
            movie = Movie.DecodeStream(this.Resources.OpenRawResource(image));
            duration = this.movie == null ? 0 : this.movie.Duration(); //获取gif持续时间
        }

        public void Start()
        {
            //if (duration > 0)
            //{
            //    this.drawed = 0;
            //    this.movieStart = 0;
            //    this.timer = new Timer();
            //    this.timer.Interval = duration;
            //    this.timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            //    this.timer.Start();
            //}
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            AppUnity.Instance.RunOnUiThread(() => this.Invalidate());
        }

        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);//执行父类onDraw方法，绘制非gif的资源
            DrawGifImage(canvas);
        }

        private int duration;
        private long drawed = 0;
        private void DrawGifImage(Canvas canvas)
        {
            //获取系统当前时间
            long nowTime = Android.OS.SystemClock.CurrentThreadTimeMillis();
            if (drawed == 0)
            {
            }
            else
            {
                //if (nowTime - drawed < duration)
                //{
                //    return;
                //}
            }

            if (movieStart == 0)
            {
                movieStart = nowTime; //若为第一次加载，开始时间置为nowTime
            }

            //获取gif当前帧的显示所在时间点
            int relTime = (int)((nowTime - movieStart) % duration);
            movie.SetTime(relTime);
            movie.Draw(canvas, 0, 0); //渲染gif图像
            drawed = nowTime;
            this.PostInvalidateDelayed(duration);
            base.OnDraw(canvas);
        }

        public void Close()
        {
            //if (this.timer != null)
            //{
            //    this.timer.Stop();
            //    this.timer.Close();
            //}
        }

    }
}

